﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using SouthApps.Framework.LoggerService;
using SouthApps.MobileHealth.SDK.Cache;
using SouthApps.MobileHealth.SDK.Services;
using SouthApps.MobileHealth.SDK.DAL.Model;

namespace MobileHealthService
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            LoggerService.Initialize(Server.MapPath(ConfigurationManager.AppSettings["LogFilePath"]), ConfigurationManager.AppSettings["LogLevel"]);

            LoggerService.Log(LogLevel.Verbose, "Initializing mobile health webservice application...");

            List<int> financiadoresIds = this.GetFinanciadores();

            this.InitializeSucursalesPrestadorCache(financiadoresIds);

            this.InitializeLocacionesServicioEspecialCache(financiadoresIds);

            this.InitializeGuardiasSucursalesPrestadorCache(financiadoresIds);

            this.InitializeCentrosAtencionCache(financiadoresIds);

            LoggerService.Log(LogLevel.Verbose, "Mobile health webservice application initialized.");
        }

        private void InitializeCentrosAtencionCache(List<int> financiadoresIds)
        {
            LoggerService.Log(LogLevel.Verbose, "Initializing centros atencion cache...");

            CentrosAtencionCache.Initialize(financiadoresIds);

            LoggerService.Log(LogLevel.Verbose, "Centros atencion cache initialized.");
        }

        private void InitializeLocacionesServicioEspecialCache(List<int> financiadoresIds)
        {
            LoggerService.Log(LogLevel.Verbose, "Initializing locaciones servicio especial cache...");

            LocacionesServicioEspecialCache.Initialize(financiadoresIds);

            LoggerService.Log(LogLevel.Verbose, "Locaciones servicio especial cache initialized.");
        }

        private void InitializeSucursalesPrestadorCache(List<int> financiadoresIds)
        {
            LoggerService.Log(LogLevel.Verbose, "Initializing sucursales prestadores cache...");

            SucursalesPrestadorCache.Initialize(financiadoresIds);

            LoggerService.Log(LogLevel.Verbose, "Sucursales prestadores cache initialized.");
        }

        private void InitializeGuardiasSucursalesPrestadorCache(List<int> financiadoresIds)
        {
            LoggerService.Log(LogLevel.Verbose, "Initializing guardias sucursales prestadores cache...");

            GuardiasSucursalPrestadorCache.Initialize(financiadoresIds);

            LoggerService.Log(LogLevel.Verbose, "Guardias sucursales prestadores cache initialized.");
        }

        private List<int> GetFinanciadores()
        {
            LoggerService.Log(LogLevel.Verbose, "Getting financiadores list...");

            FinanciadoresService financiadoresService = new FinanciadoresService();

            List<Financiador> financiadores = financiadoresService.GetFinanciadores();

            List<int> financiadoresIds = new List<int>();
            foreach (Financiador financiador in financiadores)
            {
                financiadoresIds.Add(financiador.financiadorId);
            }

            LoggerService.Log(LogLevel.Verbose, "Got financiadores list.");

            return financiadoresIds;
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        //TODO: ver el puto File Not Exists
        protected void Application_Error(object sender, EventArgs e)
        {
            //Exception ex = this.Server.GetLastError().GetBaseException();

            //LoggerService.Log(LogLevel.Error, "An unhandled exception has ocurred.", ex);
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}